package com.sgb.zhaocai.widget.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sgb.zhaocai.BR;
import com.sgb.zhaocai.R;
import com.sgb.zhaocai.entity.PickExpertDetailEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.List;

/**
 * 专家库-新增-适配器
 */
public class ExpertNewAddFieldAdapter extends BaseRecycleAdapter<PickExpertDetailEntity.Custom> {

    private FieldCallback fields;

    public void setCustomfield(FieldCallback customfield) {
        this.fields = customfield;
    }

    public ExpertNewAddFieldAdapter(Context context, List<PickExpertDetailEntity.Custom> list) {
        super(context, R.layout.item_expert_new_field, list);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        RelativeLayout delete = holder.itemView.findViewById(R.id.delete);//删除
        EditText custom_field_val = holder.itemView.findViewById(R.id.custom_field_val);//内容输入信息
        TextView edit_length = holder.itemView.findViewById(R.id.edit_length);//内容输入信息
        delete.setOnClickListener(v ->fields.FieldDeleta(position) );
        custom_field_val.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                edit_length.setText(s.toString().length()+"");
            }
        });
        PickExpertDetailEntity.Custom custom = getDatas().get(position);
        if (!TextUtils.isEmpty(custom.getVal()) && custom.getLength() == "0") {
            custom.setLength(custom_field_val.getText().length() + "");
            edit_length.setText(custom.getLength());
        }

    }
    @Override
    public void convert(ItemViewHolder holder, PickExpertDetailEntity.Custom entity) {
        holder.setBinding(BR.viewModel, entity);
    }

    public interface FieldCallback {
        void FieldDeleta(int position);
    }
}
